package com.talking.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.bumptech.glide.Glide;
import com.talking.App;
import com.talking.R;
import com.talking.bean.MomentBean;
import com.talking.bean.UserBean;
import com.talking.bean.WriteMomentPicBean;
import com.talking.databinding.ItemMomentBinding;
import com.talking.inte.ParaClick2;
import com.talking.model.Http;
import com.talking.presenter.MomentPresenter;
import com.talking.presenter.UserPresenter;
import com.talking.utils.Utils;
import com.talking.view.ImageActivity;
import com.talking.view.PersonActivity;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MomentAdapter extends ArrayAdapter<MomentBean> {
    private static final int layoutId = R.layout.item_friend;
    private final Activity context;
    private final ParaClick2 paraClick2;

    public MomentAdapter(Activity context, List<MomentBean> objects, ParaClick2 paraClick2){
        super(context,layoutId,objects);
        this.context=context;
        this.paraClick2=paraClick2;
    }
    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        final List<WriteMomentPicBean> data=new ArrayList<>();
        WriteMomentPicAdapter adp;
        MomentBean momentBean = getItem(position);
        @SuppressLint("ViewHolder") @NonNull ItemMomentBinding binding = ItemMomentBinding.inflate(LayoutInflater.from(context));
        binding.itemMomentContent.setText(momentBean.content);
        UserPresenter.getUserByAccount(momentBean.account, App.sp.getString("account", ""), new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) {
                context.runOnUiThread(() -> {
                    try {
                        JSONObject jo = new JSONObject(response.body().string());
                        if (momentBean.account.equals(App.sp.getString("account",""))){
                            binding.itemMomentNickname.setText(jo.getString("nickname"));
                        }else {
                            binding.itemMomentNickname.setText(jo.getJSONArray("friendMsg").getJSONObject(0).getString("remark"));
                        }
                        Glide.with(context).load(Http.BASE_URL+jo.getString("iconUrl")).apply(Utils.getGlideOption()).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(binding.itemMomentIcon);
                        binding.itemMomentIcon.setOnClickListener(v -> {
                            try {
                                App.userBean=new UserBean(jo.getString("nickname"),jo.getString("email"),momentBean.account,jo.getString("iconUrl"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            context.startActivity(new Intent(context, PersonActivity.class));
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            }
        });
        adp=new WriteMomentPicAdapter(context,data);
        binding.itemMomentGrid.setAdapter(adp);
        try {
            JSONArray ja = new JSONArray(momentBean.files);
            for (int i = 0; i < ja.length(); i++) {
                data.add(new WriteMomentPicBean(Http.BASE_URL+ja.getString(i)));
            }
            adp.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        binding.itemMomentGrid.setOnItemClickListener((parent1, view, position1, id) -> {
            WriteMomentPicBean bean = data.get(position1);
            Intent intent=new Intent(context, ImageActivity.class);
            intent.putExtra("img",bean.url);
            context.startActivity(intent);
        });
       if (momentBean.account.equals(App.sp.getString("account",""))){
           binding.itemMomentDelete.setOnClickListener(v -> {
               AlertDialog.Builder dialog=new AlertDialog.Builder(context);
               dialog.setTitle("提示").setMessage("您确定删除这条动态吗？").setPositiveButton("确定", (dialog1, which) -> {
                   MomentPresenter.doget("moment/delete/?id="+momentBean.id, new Callback() {
                       @Override
                       public void onFailure(@NotNull Call call, @NotNull IOException e) {
                           e.printStackTrace();
                       }
                       @Override
                       public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                           paraClick2.onClick();
                       }
                   });
               }).setNeutralButton("取消",null).show();
           });
       }else {
           binding.itemMomentDelete.setVisibility(View.GONE);
       }
        return binding.getRoot();
    }
}